﻿namespace Apache.NMS
{
    public static class NmsMessageExtensions
    {
        private const string NumberOfDeliveryAttempts = "NumberOfDeliveryAttempts";

        /// <summary>
        /// Gets the number of times the message has been attempted to be delivered.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <returns></returns>
        public static byte GetNumberOfDeliveryAttempts(this IMessage message)
        {
            if (!message.Properties.Contains(NumberOfDeliveryAttempts))
                message.Properties.SetByte(NumberOfDeliveryAttempts, 0);

            return message.Properties.GetByte(NumberOfDeliveryAttempts);
        }

        /// <summary>
        /// Increments the number of delivery attempts.
        /// </summary>
        /// <param name="message">The message.</param>
        public static void IncrementNumberOfDeliveryAttempts(this IMessage message)
        {
            var attempts = message.GetNumberOfDeliveryAttempts();
            attempts++;

            message.Properties.SetByte(NumberOfDeliveryAttempts, attempts++);
        }
    }
}
